<?
/*-- Project Introduce --*/


/**
 * 账号
 *
 * @author		alee
 * @access		public
 */
class JJ_Account
{
	/**
	 * 构造函数
	 *
	 * @access	public
	 * @return	void
	 */
	public function JJ_Account($sID,$sServer=null,$sRCName='')
	{
		$this->sID = $sID ;
		$this->sServerName = ($sServer===null)? self::GetDefaultServer(): $sServer ;
		$this->sRCName = $sRCName? $sRCName: self::GetClientResourceName() ;
	}

	/**
	 * 构造函数，通过一个 字符串 格式的账号返回 JJ_Account对象
	 *
	 * @access	public
	 * @param	$sJabberAddress	string
	 * @static
	 * @return	JJ_Account
	 */
	static public function CreateFromString($sJabberAddress)
	{
		$arrRes = array() ;
		if( !preg_match('/([\w\.\-_#]+)@([\w\.\-_]+)(\/([\w]+))?/i',$sJabberAddress,$arrRes) )
		{
			return null ;
		}
		
		@list($sAddress,$sID,$sServer,,$sRCName) = $arrRes ;
		return new JJ_Account($sID,$sServer,$sRCName) ;
	}
	
	/**
	 * 构造函数，通过 用户名 格式的账号返回 JJ_Account对象
	 *
	 * @access	public
	 * @param	$sUsername	string
	 * @static
	 * @return	JJ_Account
	 */
	static public function CreateFromUsername($sUsername)
	{
		$sID = self::GetDefaultIDPrefix().$sUsername.self::GetDefaultIDPostfix() ;
		return new JJ_Account($sID) ;
	}
	
	/**
	 * 校验 格式
	 *
	 * @access	public
	 * @param	$sJabberAddress	string
	 * @static
	 * @return	bool
	 */
	static public function VerifyJabberAddress($sJabberAddress)
	{
		return (self::CreateFromString($sJabberAddress)!==null) ;
	}
	
	/**
	 *	
	 *
	 * @access	public
	 * @return	string
	 */
	public function __toString()
	{
		return $this->GetJabberAddress() ;
	}

	/**
	 * 取得服务器名称
	 *
	 * @access	public
	 * @param	$Parameter
	 * @return	string
	 */
	public function GetServer()
	{
		return $this->sServerName ;
	}
	
	/**
	 * 
	 *
	 * @access	public
	 * @return	string
	 */
	public function GetID()
	{
		return $this->sID ;
	}
	
	/**
	 * 取得账号的资源名称
	 *
	 * @access	public
	 * @return	string
	 */
	public function GetResourceName()
	{
		return $this->sRCName ;
	}
	
	/**
	 * 
	 *
	 * @access	public
	 * @param	$bWithRCName=false		bool
	 * @return	string
	 */
	public function GetJabberAddress($bWithRCName=false)
	{
		$sAddress = $this->GetID().'@'.$this->GetServer() ;
		if( $bWithRCName and $this->GetResourceName() )
		{
			$sAddress.= '/'.$this->GetResourceName() ;
		}
		
		return strtolower($sAddress) ;
	}
	
	/**
	 * 返回显示名称
	 *
	 * @access	public
	 * @param	$sContactName		string
	 * @return	string
	 */
	public function GetDisplayName($sContactName)
	{
		if( strtolower($this->sID)==strtolower($sContactName) )
		{
			return $this->GetUsername() ;
		}
		
		else
		{
			return $sContactName ;
		}
	}

	/**
	 * 
	 *
	 * @access	public
	 * @return	string
	 */
	public function GetUsername()
	{
		$sUsername = $this->GetID() ;
		$sPrefix = self::GetDefaultIDPrefix() ;
		$sPostfix = self::GetDefaultIDPostfix() ;
		
		// 前缀
		if( $nLen = strlen($sPrefix) )
		{
			if( strtolower(substr($sUsername,0,$nLen))==strtolower($sPrefix) )
			{
				$sUsername = substr($sUsername,$nLen) ;
			}
		}
		
		// 后缀
		if( $nLen = strlen($sPostfix) )
		{
			if( strtolower(substr($sUsername,-$nLen))==strtolower($sPostfix) )
			{
				$sUsername = substr($sUsername,0,strlen($sUsername)-$nLen) ;
			}
		}
		
		return $sUsername ;
	}
	
	/**
	 * 取得资源名称
	 *
	 * @access	public
	 * @static
	 * @return	string
	 */
	static public function GetClientResourceName()
	{
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		return $aSetting->GetItem('/JJ/','ClientResourceName') ;
	}

	/**
	 * 取得缺省 Jabber 服务器
	 *
	 * @access	public
	 * @static
	 * @return	string
	 */
	static public function GetDefaultServer()
	{
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		return $aSetting->GetItem('/Jabber/','ServerHost') ;
	}
	
	/**
	 * 取得缺省 Jabber ID 前缀
	 *
	 * @access	public
	 * @static
	 * @return	string
	 */
	static public function GetDefaultIDPrefix()
	{
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		return $aSetting->GetItem('/Jabber/','JabberIDPrefix') ;
	}
	
	/**
	 * 取得缺省 Jabber ID 后缀
	 *
	 * @access	public
	 * @static
	 * @return	string
	 */
	static public function GetDefaultIDPostfix()
	{
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		return $aSetting->GetItem('/Jabber/','JabberIDPostfix') ;
	}
	
	// 
	private $sRCName ;
	private $sServerName ;
	private $sID ;
}

?>